package com.ruoyi.Utils;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.base.domain.MaterialsParams;
import com.ruoyi.base.domain.SjzMaterials;
import com.ruoyi.common.utils.http.HttpUtils;
import org.aspectj.apache.bcel.classfile.annotation.NameValuePair;


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MaterialsUtil {

    /**
     * 获取收藏品
     * @return
     */
    public static List<SjzMaterials> getSjzMaterialsNo(MaterialsParams materialsParams) {
        List<SjzMaterials> list = new ArrayList<>();

         String result = HttpUtils.sendSJZPost(materialsParams.getUrl(),materialsParams.getParams(),materialsParams.getContentType(),materialsParams.getCookie());

        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONObject jData = jsonObject.getJSONObject("jData");
        JSONObject jDataData = jData.getJSONObject("data");
        JSONObject jDataDataData = jDataData.getJSONObject("data");
        // Get the "list" array
        JSONArray listArray = jDataDataData.getJSONArray("list");
        // Iterate through the array
        for (int i = 0; i < listArray.size(); i++) {
            JSONObject itemObject = listArray.getJSONObject(i);
            // Extract data from the JSON object
            Long objectID = itemObject.getLong("objectID");
            String objectName = itemObject.getString("objectName");
            Integer length = itemObject.getInteger("length");
            Integer width = itemObject.getInteger("width");
            Integer grade = itemObject.getInteger("grade");
            Double weight = itemObject.getDouble("weight");
            // Create a SjzMaterials object and set its properties
            String primaryClass = itemObject.getString("primaryClass");
            String secondClass = itemObject.getString("secondClass");
            String secondClassCN = itemObject.getString("secondClassCN");
            String desc = itemObject.getString("desc");
            String pic = itemObject.getString("pic");
            String prePic = itemObject.getString("prePic");
            Double avgPrice = itemObject.getDouble("avgPrice");
            // Create a SjzMaterials object and set its properties
            SjzMaterials SjzMaterials = new SjzMaterials();
            SjzMaterials.setObjectId(objectID);
            SjzMaterials.setObjectName(objectName);
            SjzMaterials.setLength(length);
            SjzMaterials.setWidth(width);
            SjzMaterials.setGrade(grade);
            SjzMaterials.setWeight(weight);
            SjzMaterials.setPrimaryClass(primaryClass);
            SjzMaterials.setSecondClass(secondClass);
            SjzMaterials.setSecondClassCn(secondClassCN);
            SjzMaterials.setDesc(desc);
            SjzMaterials.setPic(pic);
            SjzMaterials.setPrePic(prePic);
            SjzMaterials.setAvgPrice(avgPrice);

            // Add to the list
            list.add(SjzMaterials);
        }
        return list;
    }
}
